import config from "@/config/index.js";

let baseUrl = config.baseUrl;
// const baseUrl = 'https://hsy.sunsetbetter.com/live-hub/api/vod/v1/';
import utils from "@/common/utils/utils.js";
let isModule = uni.getStorageSync('isModule')?true:false

/**
 * 请求数据封装方法
 * @param url 请求地址（login/login）
 * @param Head header对象
 * @param data 请求对象
 * @param loading 是否展示loading
 * @param loadingTitle 展示loading文字
 * @returns {Promise<unknown>}
 */
let port = uni.getSystemInfoSync().platform
/**
 * @param port android 是安卓
 * @param port ios 是 ios
 * @param port devtools  是微信小程序
 */
function fetch(method, url, data, loading, loadingTitle, paramsUrl) {
    data.token = uni.getStorageSync('token')?uni.getStorageSync('token'):0
	// data.kor_type = uni.getStorageSync('kor_type')?uni.getStorageSync('kor_type')=='kor'?0:1:''
	return new Promise((resolve, reject) => {	
		//显示加载动画
		
		const params = {
			...data
		};
		const Head = {}
		uni.request({
			url,
			method,
			data: params,
			header: {
				// 数据被编码为名称/值对
				"content-type": "application/json",
				"token": uni.getStorageSync('token')?uni.getStorageSync('token'):'',
				"Access-Control-Allow-Origin":'*'
			},
			success: res => {
				let code = res.data.code;
				let message = res.data.message || '';
				switch (Number(code)) {
				    case 200:
				    	resolve(res.data);
                        break;
				    case 0:	
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
				    	reject(res.data);
						
				    	break;
					case 1:
						resolve(res.data);
						break;
					case -99:
						reject(res.data)	
						uni.setStorageSync('token' , '')
						uni.setStorageSync('isModule' , true)
						uni.showModal({
						  title: '登录',
						  content: '登录后可使用完整功能',
						  cancelText: "取消", // 取消按钮的文字  
						  confirmText: "确认", // 确认按钮的文字  
						  showCancel: true, // 是否显示取消按钮，默认为 true
						  success: function (res) {
							  uni.setStorageSync('isModule' , false)
						    if (res.confirm) {
						      uni.navigateTo({
						      	url:'/pages/login/login'
						      })
						    } else if (res.cancel) {
						      uni.switchTab({
						      	url:'/pages/index/index'
						      })
						    }
						  }
						});
						break;
					case 500:
						reject(res.data)	
						break;
				    case 1:
				    	reject(res.data)
				    	break;
					default:
						reject(res);
						break;
				}
			},
			fail: err => {
				reject(err);
				console.log(err , '接口报错相关')
                uni.showToast({
                    title:err.errMsg?err.errMsg:"ERROR",
                    icon:"none"
                })
			},
			complete: () => {
				setTimeout(function() {
					uni.stopPullDownRefresh(); //停止下拉刷新动画
				}, 1000);
				if (loading) {
					setTimeout(() => {
						uni.hideLoading();
					}, 1000)
				}
			},
		});
	});
}

export default {
	get(url, params, loading, loadingTitle, obs) {
		if(config.isEnv == false){
			baseUrl = window.location.protocol + "//" + window.location.host + '/admin/public/index.php/'	
		}
		return fetch("GET", baseUrl + url, params, loading, loadingTitle, url);
	},
	post(url, params, loading, loadingTitle , obs , apitype) {
		if(config.isEnv == false){
			baseUrl = window.location.protocol + "//" + window.location.host + '/admin/public/index.php/'	
		}
		return fetch("POST", baseUrl + url, params, loading, loadingTitle, url , apitype);
	},

};
